<?php
namespace app\clubadmin\controller;
use \think\Controller;
use \think\Db;
use \think\File;

class ClubVideo extends Common
{
public function index(){
    $clubs=Db::name("clubs")->select();
    $this->assign("clubs",$clubs);

    $clubvideos=Db::name("clubvideos")->select();
    $this->assign("clubvideos",$clubvideos);
    return $this->fetch();
  }

public function jiekou(){
   $rs=db("clubvideos")->select();
   return json($rs);
}

  public function add(){
    $file=request()->file('pic');
    $info="";
    if($file){
       $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'img');
      }
      $data=array();
    if (request()->isPost()) {
     //创建数组，给video表的所有字段赋值
      $data=[
       
        'cv_id'=>input('param.cv_id'),
        'cv_name'=>input('param.cv_name'),
        'cv_pic'=>$info->getFilename(),
         'cv_intro'=>input('param.cv_intro'),
          'cv_address'=>input('param.cv_address'),
          /*'hits'=>input('param.hits'),*/
      ];

     $validate = \think\Loader::validate('ClubVideo');
if(!$validate->check($data)){
    $this->error($validate->getError());
}
      //存入数据库
    $res=Db::name("clubvideos")->insert($data);
    //处理结果
    if ($res) {
      $this->success("添加视频成功");
    }else{
      $this->error("添加视频失败");
    }
    }
  }
   public function delete($id){
    //先删除头像：find方法和unlink
    if (Db::name("clubvideos")->delete($id)) 
      {  
        echo Db::name("clubvideos")->getLastSql();exit;
      $this->success('删除成功');
    }
    else $this->error('删除失败');
  }

 public function update(){
    if (request()->isPost()) {
      $file=request()->file('cv_pic');

      if ($file) {
        $cv_id=input('param.cv_id');
        $picname_DB=Db::name("clubvideos")->where('cv_id',$cv_id)->column('cv_pic');  
        //dump($picname_DB);
        $file_name="C:wamp\www\\"."sport\public\static\img\\".$picname_DB[0];
        /*dump($file_name);exit;*/
        /*dump($file_name);
        dump(file_exists($file_name));exit;*/
        /*echo $file_name;
        exit;*/
        if (file_exists($file_name)) {          
          unlink($file_name);
          $info = $file->rule('uniqid')->validate(['size'=>5000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'img');

          if ($info) {
            $data=[
              'cv_id'=>input('param.cv_id'),
        'cv_name'=>input('param.cv_name'),
        'cv_pic'=>$info->getFilename(),
         'cv_intro'=>input('param.cv_intro'),
          'cv_address'=>input('param.cv_address'),
            ];
            $res=Db::name("clubvideos")->update($data);    
            if ($res) {
              $this->success("修改成功");

            }else{
              $this->error("修改失败");
            }

          }

        }else $this->error('数据库中无指定图片');
      }else $this->error('未获取图片');
    }else $this->error('未获取数据');
  }
   public function edit(){
    $bid=input('cv_id');
    $clubvideos=Db::name("clubvideos")->find($bid);
    echo json_encode($clubvideos);
  }
}